package cn.wyz.org.controller;

import cn.wyz.basic.exception.BusinessException;
import cn.wyz.basic.util.AjaxResult;
import cn.wyz.basic.util.ExcelUtil;
import cn.wyz.basic.util.PlusQueryList;
import cn.wyz.org.domain.Shop;
import cn.wyz.org.domain.ShopAuditLog;
import cn.wyz.org.query.ShopQuery;
import cn.wyz.org.service.IShopService;
import cn.wyz.org.vo.ShopVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop")
@Api(value = "商品的API",description="商品相关的CRUD功能")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.insert(shop);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.deleteById(id);
            return AjaxResult.make();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.selectById(id);
    }

    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.selectAll();
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PlusQueryList<Shop> queryData(@RequestBody ShopQuery query) {
        return shopService.queryData(query);
    }

    /**
     * 店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                //这个方法是纯粹的店铺新增,管理员修改
                shopService.settlement(shop);
            } else{
                shop.setState(0);
                shopService.update(shop);
            }
            return AjaxResult.make();
        } catch (BusinessException e) { //这是我们自定义的业务异常,需要我们自己去处理
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 店铺审核通过
     */
    @PostMapping("/audit/pass")
    public AjaxResult ass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.make();
        } catch (BusinessException e) { //这是我们自定义的业务异常,需要我们自己去处理
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 店铺审核驳回/shop/audit/reject
     */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.make();
        } catch (BusinessException e) { //这是我们自定义的业务异常,需要我们自己去处理
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
     * 邮件激活
     */
    @GetMapping ("/activation/{shopId}")
    public String activation(@PathVariable("shopId") Long shopId){
        shopService.activation(shopId);
         return "激活成功";
    }

    /**
     * 导出excel表
     * @param response
     */
    @GetMapping("/export")
    public void export( HttpServletResponse response){
        try {
            List<Shop> shops = shopService.selectAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入Excel表
     * RequestPart上传文件用这个,藏在请求中发出去
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        //String file = "F:\\文件名.xls";
        //解析excel，
        List<Shop> list = ExcelUtil.importExcel(file,0,1,Shop.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入
        System.out.println("导入数据一共【"+list.size()+"】行");
        //保存到数据库中[自己实现，提示：动态sql 批量插入]
        shopService.patchInsert(list);
    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> idList){
        try {
            shopService.patchDelete(idList);
            return AjaxResult.make();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.make()
                    .setSuccess(false)
                    .setMessage("烂中烂,直接开摆!");
        }
    }

    /**
     * 驳回重新填写页面回显
     * @return
     */
    @GetMapping("/reset/{id}")
    public Shop reset(@PathVariable("id") Long id) {
        return shopService.reset(id);
    }

    /**
     * 报表显示
     * @return
     */
    @GetMapping("/echarts")
    public Map<String,Object> echarts(){
        List<ShopVo> voList = shopService.countByState();
        List<Integer> states = voList.stream().map(ShopVo::getState).collect(Collectors.toList());
        List<Integer> nums = voList.stream().map(ShopVo::getCountNum).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }
}
